var Promise = require('../utils/es6-promise.min.js');
var Api = require('lmfService.js');
const CONFIG = require('../config.js');
var navigateTo = function (value = '', success_type = 'event') {
  wx.navigateTo({
    url: `/pages/user/login?success_type=${success_type}&success_value=${value}`,
  })
}
var getUserInfo = function () {
  var openid = wx.getStorageSync('openid');
  var user_uid = wx.getStorageSync('lmf-user_uid');
  var user_name = wx.getStorageSync('lmf-user_name') || '';
  var user_phone = wx.getStorageSync('lmf-user_phone');
  var session = wx.getStorageSync('session:sso');



  if (!user_uid || !user_phone || !session) {
    return false;
  }
  return {
    user_uid: user_uid,
    user_name: user_name || user_phone,
    user_phone: user_phone,
    openid: openid
  }
}

var success = (success_type, success_value) => {
  var currentPage = (function (pages) {
    return pages[pages.length - 1]
  })(getCurrentPages())
  if (!currentPage) {
    wx.navigateTo({
      url: '/pages/user/index',
    });
    return false;
  }
  if (success_type == 'event') {
    try { currentPage[success_value](); }
    catch (e) {
      console.error('执行登录事件回调失败', e);
    }
  } else {
    wx.navigateTo({
      url: success_value || '/pages/user/index',
    })
  }
}


var tryLogin = function () {
  var openid = wx.getStorageSync('openid');
  return Api.sso
    .login()
    .then((res) => {

      return Api.post("/user/fetchUserInfo", {
        app_id: CONFIG.appid,
        open_id: openid,
        alias: CONFIG.alias
      })
    })
    .then((userInfo) => {
      wx.setStorageSync('lmf-user_name', userInfo.name);
      wx.setStorageSync('lmf-user_uid', userInfo.userUid);
      wx.setStorageSync('lmf-user_phone', userInfo.phone);
      var headImageUrl = userInfo.wechatInfo
        .map(function (wechatInfo) {
          return wechatInfo.headImgUrl;
        })
        .filter(function (headImgUrl) {
          return headImgUrl != '';
        })[0]



      if (headImageUrl) {
        wx.setStorageSync('lmf-user_head_image_url', headImageUrl);
      }
      return {
        user_uid: userInfo.userUid,
        user_name: userInfo.name || userInfo.phone,
        user_phone: userInfo.phone,
        openid: openid
      }
    })
}

var userService = {
  eventExc(eventName) {
    if (eventName == '') {
      console.error({ code: 3000, error_msg: '回调事件名不能为空' });
      return false
    }
    var userInfo = getUserInfo();
    if (!userInfo) {
      tryLogin()
        .then(() => {
          success('event', eventName)
        })
        .catch(function (error) {
          navigateTo(eventName);
        })

      return false;
    }

    return userInfo;

  },
  gotoPage(pageName) {
    if (pageName == '') {
      console.error({ code: 3000, error_msg: '页面地址不能为空' });
      return false
    }
    var userInfo = getUserInfo();
    if (!userInfo) {
      tryLogin()
        .then(() => {
          return success('page', pageName)
        })
        .catch(function (error) {
          navigateTo(pageName, 'page');
        })
      return false;
    }
    return success('page', pageName);
  }
};
module.exports = userService;